﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using SharedLibraries;
using SharedLibraries.Constants;
using EntityObject;
using ThangNM_LIB.DB;
using ThangNM_LIB.FUNC;
using DataAccessObject;
using ThangNM_LIB.EO;

namespace ThangNM.Admin
{
    public partial class SiteMapUI : System.Web.UI.Page
    {
        public List<SEOEO> lstBL
        {
            get { return (List<SEOEO>)ViewState["lstBL"]; }
            set { ViewState["lstBL"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Title_FormUC1.setTitle(ConstEx.Table_Name_C.Sitemap, ConstEx.funcButtonToolBar.LIST);
                MsgUC1.setClearMsg();
                if (!IsPostBack)
                {
                    if ((new ShareDAO.UserDAO()).SelectBy_sPermission(Session["secret"], Convert.ToString(Request.Url.AbsolutePath), ConstEx.funcButtonToolBar.MAIN) == false)
                    {
                        ToolBarUC1.Visible = false;
                        grvList.Visible = false;
                        Commons.setAccessDenied(Page, MsgUC1);
                        return;
                    }
                    lstBL = new List<SEOEO>();
                    DataTable dt = (new SEODAO()).SelectList();
                    lstBL = (List<SEOEO>)ConvertEx.ConvertDataTableToListObject<SEOEO>(dt, lstBL.GetType());
                    BindData();
                }
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        //public static string ConvertSiteMap2ListObject(string input)
        //{
        //    input = input.Replace(XMLSEO.str_save_opentag, XMLSEO.str_temp_opentag);
        //    input = input.Replace(XMLSEO.str_save_closetag, XMLSEO.str_temp_closetag);
        //    return input;
        //}

        //public static string ConvertListObject2SiteMap(string input)
        //{
        //    input = input.Replace(XMLSEO.str_temp_opentag, XMLSEO.str_save_opentag);
        //    input = input.Replace(XMLSEO.str_temp_closetag, XMLSEO.str_save_closetag);
        //    return input;
        //}

        protected void EventButtonToolBar_Click(object sender, CustomerEventArgs e)
        {
            switch (e.iType)
            {
                case ConstEx.funcButtonToolBar.INSERT_LIST: break;
                case ConstEx.funcButtonToolBar.INSERT_ITEM: break;
                case ConstEx.funcButtonToolBar.UPDATE_ITEM:
                    SaveChange();
                    break;
                case ConstEx.funcButtonToolBar.DELETE_LIST: break;
                case ConstEx.funcButtonToolBar.DELETE_ITEM: break;
                case ConstEx.funcButtonToolBar.REFRESH_LIST: break;
                case ConstEx.funcButtonToolBar.EXPORT_EXCEL: break;
                case ConstEx.funcButtonToolBar.RESET_FORM: break;
            }
        }

        protected void SaveChange()
        {
            try
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.INFO, MessagesEx.CHUC_NANG_NAY_DANG_TAM_KHOA);
                return;
                //System.IO.File.WriteAllText(Server.MapPath("~/sitemap.xml"), ConvertListObject2SiteMap(Commons.ListObject2Xml(lstBL, "")));
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, "Cập nhật thành công.");
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                TextBox txtloc_Temp = (TextBox)grvList.HeaderRow.FindControl("txtloc_Temp");
                TextBox txtlastmod_Temp = (TextBox)grvList.HeaderRow.FindControl("txtlastmod_Temp");
                DropDownList ddlchangefreq_Temp = (DropDownList)grvList.HeaderRow.FindControl("ddlchangefreq_Temp");
                TextBox txtpriority_Temp = (TextBox)grvList.HeaderRow.FindControl("txtpriority_Temp");
                DropDownList ddlstatus_Temp = (DropDownList)grvList.HeaderRow.FindControl("ddlstatus_Temp");
                if (String.IsNullOrEmpty(txtloc_Temp.Text))
                {
                    MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, "loc không được để trống.");
                    txtloc_Temp.Focus();
                    return;
                }
                SEOEO objBL = new SEOEO();
                objBL.sUrlRewrite = OtherEx.RemoveDomainInUrl(txtloc_Temp.Text);
                objBL.tDateTimeModify = Convert.ToDateTime(txtlastmod_Temp.Text);
                objBL.iChangefreq = Convert.ToInt16(ddlchangefreq_Temp.SelectedValue);
                objBL.fPriority = Convert.ToDouble(txtpriority_Temp.Text);
                objBL.bStatus = Convert.ToBoolean(ddlstatus_Temp.SelectedValue);
                lstBL.Insert(0, objBL);
                txtloc_Temp.Text = "";
                txtlastmod_Temp.Text = "";
                ddlchangefreq_Temp.SelectedIndex = -1;
                txtpriority_Temp.Text = "";
                ddlstatus_Temp.SelectedIndex = -1;

                BindData();
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, "Thêm thành công.");
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void BindData()
        {
            grvList.DataSource = lstBL.ToList();
            grvList.DataBind();
            BindData_DDL();
        }

        protected void BindData_DDL()
        {
            DropDownList ddlchangefreq_Temp = (DropDownList)grvList.HeaderRow.FindControl("ddlchangefreq_Temp");
            if (ddlchangefreq_Temp != null)
            {
                ddlchangefreq_Temp.DataSource = GetListConstants.ChangeFreq_GLC();
                ddlchangefreq_Temp.DataValueField = "Key";
                ddlchangefreq_Temp.DataTextField = "Value";
                ddlchangefreq_Temp.DataBind();
            }

            DropDownList ddltype_Temp = (DropDownList)grvList.HeaderRow.FindControl("ddltype_Temp");
            if (ddltype_Temp != null)
            {
                ddltype_Temp.DataSource = GetListConstants.Sitemap_Type_GLC();
                ddltype_Temp.DataValueField = "Key";
                ddltype_Temp.DataTextField = "Value";
                ddltype_Temp.DataBind();
            }

            DropDownList ddlstatus_Temp = (DropDownList)grvList.HeaderRow.FindControl("ddlstatus_Temp");
            if (ddlstatus_Temp != null)
            {
                ddlstatus_Temp.DataSource = GetListConstants.Sitemap_Status_GLC();
                ddlstatus_Temp.DataValueField = "Key";
                ddlstatus_Temp.DataTextField = "Value";
                ddlstatus_Temp.DataBind();
            }
        }

        protected void grvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdDelete")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                lstBL.RemoveAt(id);
                BindData();
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, "Xóa thành công.");
            }
        }

        protected void grvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddlchangefreq = (DropDownList)e.Row.FindControl("ddlchangefreq");
                Label lblchangefreq = (Label)e.Row.FindControl("lblchangefreq");
                ddlchangefreq.DataSource = GetListConstants.ChangeFreq_GLC();
                ddlchangefreq.DataValueField = "Key";
                ddlchangefreq.DataTextField = "Value";
                ddlchangefreq.DataBind();
                ddlchangefreq.SelectedValue = lblchangefreq.Text;

                DropDownList ddltype = (DropDownList)e.Row.FindControl("ddltype");
                Label lbltype = (Label)e.Row.FindControl("lbltype");
                ddltype.DataSource = GetListConstants.Sitemap_Type_GLC();
                ddltype.DataValueField = "Key";
                ddltype.DataTextField = "Value";
                ddltype.DataBind();
                ddltype.SelectedValue = lbltype.Text;

                DropDownList ddlstatus = (DropDownList)e.Row.FindControl("ddlstatus");
                Label lblstatus = (Label)e.Row.FindControl("lblstatus");
                ddlstatus.DataSource = GetListConstants.Sitemap_Status_GLC();
                ddlstatus.DataValueField = "Key";
                ddlstatus.DataTextField = "Value";
                ddlstatus.DataBind();
                ddlstatus.SelectedValue = lblstatus.Text;
            }
        }
    }
}